package com.doit.day03;

import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.sql.*;
import java.time.Duration;
import java.util.*;

public class ConsumerExerciseMysql {
    public static void main(String[] args) throws Exception {
        //jdbc连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        PreparedStatement pps = conn.prepareStatement("insert into guid values(?)");
        PreparedStatement pps1 = conn.prepareStatement("select count(distinct guid) as cnt from guid");

        //1.创建一个消费者==》过
        //开启了一个收集数据的线程
        new Thread(new ConsumerDataThreadMysql(pps,"event-log")).start();


        //这个线程是返回结果的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //3.每隔5s返回一次结果  ==》 timer
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //返回去重之后的个数，guid算uv
                        ResultSet resultSet = null;
                        try {
                            resultSet = pps1.executeQuery();
                            while (resultSet.next()){
                                long cnt = resultSet.getLong(1);
                                System.out.println(cnt);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }



                    }
                },1000,5000);
            }
        }).start();



    }
}
class ConsumerDataThreadMysql implements Runnable{
    private PreparedStatement pps;
    private KafkaConsumer<String, String> consumer;


    public ConsumerDataThreadMysql(PreparedStatement pps,String ... topic){
        Map<String, Object> map = new HashMap<>();
        map.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        map.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        map.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        map.put(ConsumerConfig.GROUP_ID_CONFIG,"g02");
        map.put(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        map.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        map.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        consumer = new KafkaConsumer<String, String>(map);
        consumer.subscribe(Arrays.asList(topic));
        this.pps = pps;
    }


    @Override
    public void run() {
        while (true){
            //拉取数据
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> record : poll) {
                //获取到kafka中的数据
                String json = record.value();
                EventLog eventLog = JSON.parseObject(json, EventLog.class);
                //2.不断地拉取数据，收集我们的guid，而且还要对guid进行去重 ==》 用什么去收集  HashSet
                //将guid放到mysql数据库中
                try {
                    pps.setInt(1,eventLog.getGuid());
                    pps.execute();
                } catch (SQLException e) {
                    e.printStackTrace();
                }


            }
        }
    }
}
